const app = getApp();
Page({
  data: {
    showTopTips: false,
    errMsg: "",
    checkTypes: ["维保检查", "日常巡查", "定期检查"],
    checkTypeIndex: 0,

    isAgree: false,
    maxPage: 9,
    files: [],
    filespath:[],
    inputLength:0,
    inputMaxLength:200
  },
  showTopTips: function () {
    var that = this;
    this.setData({
      showTopTips: true
    });
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 3000);
  },
  bindCheckTypeChange: function (e) {
    console.log('picker country 发生选择改变，携带值为', e.detail.value);
    this.setData({
      checkTypeIndex: e.detail.value
    })
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: this.data.maxPage,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        //console.log(tempFilePaths.length);
        uploadFiles(that, tempFilePaths);
        // that.setData({
        //   files: that.data.files.concat(res.tempFilePaths)
        // });
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  formSubmit: function (e) {
    var _this = this
    /*********************    
    wx.redirectTo({
      url:'create_photo'
    })
    **********************/
    console.log(e.detail.value);
    wx.request({
      url: app.globalData.serverBase + '/fault/add/',
      data:
      {
        checkType: e.detail.value.checkType,
        result: e.detail.value.result,
        memo: e.detail.value.memo
      }
      ,
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      header: {
        'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
      },
      success: function (res) {
        var err = res.data.data.status
        console.log(typeof (err))
        if (status === 0) {
          console.log("error")
          _this.setData({
            error: err,
            errMsg: res.data.data.message
          })
          _this.showTopTips();
        }
        else {
          wx.showToast({
            title: '成功',
            icon: 'success',
            duration: 2000
          })
          //跳转至指定页面
          // wx.redirectTo({
          //   url: 'create_photo'
          // })
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },
  formReset: function (e) {
    console.log('form发生了reset事件，携带数据为：', e.detail.value)
    this.setData({
      chosen: '',
      files: [],
      filespath: []
    })
  },
  bindInput:function (e) {
    var inputvalue = e.detail.value;
    if (inputvalue.length > this.data.inputMaxLength) return;
    this.setData({
      inputLength:inputvalue.length
    });
  }
});

var uploadFiles = function (page, path) {
  wx.showToast({
    icon: "loading",
    title: "正在上传"
  })
  for (var i = 0; i < path.length; ++i) {
    var file_path = path[i];    
    wx.uploadFile({
      url: app.globalData.serverBase + '/fault/uploadfile/',
      filePath: file_path,
      name: 'uploadfile',
      header: { "Content-Type": "multipart/form-data" },
      formData: {
        //和服务器约定的token, 一般也可以放在header中
        'imgindex':i
      },
      success: function (res) {
        //console.log(res);
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
          return;
        }
        var data = res.data
        var files = JSON.parse(data).data;        
              
        page.setData({  //上传成功修改显示头像
          files: page.data.files.concat(file_path),
          filespath: page.data.filespath.concat(files[0].file_url)
        })       
      },
      fail: function (e) {
        console.log(e);
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
        wx.hideToast();  //隐藏Toast
      }
    })
  }

}